package com.controller;


import microsoft.aspnet.signalr.client.LogLevel;
import microsoft.aspnet.signalr.client.Logger;
import microsoft.aspnet.signalr.client.hubs.HubConnection;
import microsoft.aspnet.signalr.client.hubs.HubProxy;
import microsoft.aspnet.signalr.client.hubs.SubscriptionHandler1;
import microsoft.aspnet.signalr.client.transport.ServerSentEventsTransport;

import java.util.Scanner;

/**
 * Created by leohe on 2017/7/27.
 */
public class testController {

    public static void main(String[] arg){
        String ServerURI = "*********************";
        // Type=Broswer&Name=JavaClient : 连接时输入的参数
        HubConnection Connection = new HubConnection(ServerURI, "Type=Broswer&Name=JavaClient", true, new Logger() {
            @Override
            public void log(String message, LogLevel level) {
                //System.out.println(level.toString() + " - " + message);
            }
        });
        HubProxy hubProxy = Connection.createHubProxy("messageHub");

        //监听GetServerList方法
        hubProxy.on("GetServerList",new SubscriptionHandler1(){
            @Override
            public void run(Object p1) {
                System.out.println(p1.toString());
            }
        }, String.class);
        //监听ReciveMessage方法
        hubProxy.on("ReciveMessage",new SubscriptionHandler1(){
            @Override
            public void run(Object p1) {
                System.out.println(p1.toString());
            }
        }, String.class);

           Connection.start(new ServerSentEventsTransport(Connection.getLogger()));


        Scanner inputReader = new Scanner(System.in);
        String line = inputReader.nextLine();
        while (!"exit".equals(line)) {
            //调用 signalr 的clear方法
            hubProxy.invoke("clear");
            line = inputReader.next();
        }

        inputReader.close();
        Connection.stop();
    }
}
